//
//  CityViewController.m
//  PTravel
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "CityViewController.h"

#import "CityView.h"
@interface CityViewController ()<CityViewDelegate>
@property (retain,nonatomic)CityView *cityV;

@end


@implementation CityViewController

- (void)dealloc
{
    [_cityV release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction)] autorelease];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"选择" style:UIBarButtonItemStyleDone target:self action:@selector(changeAction)] autorelease];
    self.lat = [[NSUserDefaults standardUserDefaults] objectForKey:@"lat"];
    self.lng = [[NSUserDefaults standardUserDefaults] objectForKey:@"lng"];
    [self cityAction];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.cityV = [[CityView alloc] initWithFrame:CGRectMake(0, 64, BOUNDS.size.width, BOUNDS.size.height)];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.cityV.cityViewDelegate = self;
    [self.view addSubview:self.cityV];
    [_cityV release];
    self.navigationItem.title = @"出去走走";
    
    
}
#pragma mark === 返回按钮
- (void)buttonAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

#pragma mark === 选择按钮
- (void)changeAction
{
    TravelingViewController *travel = [[TravelingViewController alloc] init];
    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:travel];
    [self presentViewController:na animated:YES completion:^{
        
        
    }];
    [na release];
}

- (void)cityAction
{
   
    [AppTools getAFN:[NSString stringWithFormat:@"http://open.qyer.com/qyer/onroad/city_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&device_id=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&device_type=ios&guide_mode=1&lat=%@&lon=%@&page=1&track_app_channel=App%%2520Store&track_app_version=6.8.2&track_device_info=iPhone7%%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%%25209.1&v=1",self.lat, self.lng] block:^(id result) {
        NSMutableDictionary *bigDic = result;
        self.cityV.city = [[CityDetail alloc] init];
        [self.cityV.city setValuesForKeysWithDictionary:[bigDic objectForKey:@"data"]];
        NSURL *url = [NSURL URLWithString:self.cityV.city.photo];

        [self.cityV.bgImage sd_setImageWithURL:url placeholderImage:IMAGE];
        
        self.cityV.cnname.text = self.cityV.city.cnname;
        self.cityV.enname.text = self.cityV.city.enname;
        
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:self.cityV.city.timezone.intValue*100];
        NSLog(@"%@",timeZone);
        [formatter setTimeZone:timeZone];
        NSString *loctime = [formatter stringFromDate:date];
        NSLog(@"%@",loctime);
        self.cityV.timeLabel.text = loctime;
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
